package com.lhd._05weibo.controller;

import com.lhd._05weibo.mapper.CommentMapper;
import com.lhd._05weibo.pojo.dto.CommentDTO;
import com.lhd._05weibo.pojo.entity.Comment;
import com.lhd._05weibo.pojo.vo.UserVO;
import com.lhd._05weibo.common.response.JsonResult;
import com.lhd._05weibo.common.response.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @author lhd
 * @date 2024/4/23
 * @apiNote
 */
@Api(tags = "03.评论模块")
@RestController
@RequestMapping(value = "/v1/comment/")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;
    @ApiOperation(value ="发表评论功能")
    @PostMapping("insert")
    @ApiImplicitParam(name="commentDTO",value = "CommentDTO",required = true,dataType = "CommentDTO")
    public JsonResult insert(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session) {

        UserVO userVO = (UserVO)session.getAttribute("user");
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }

        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO, comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());

        commentMapper.insert(comment);
        return JsonResult.ok();
    }

    /**
     * 2. 评论列表功能
     */
    @ApiOperation(value ="评论列表功能")
    @GetMapping("selectByWeiboId")
    @ApiImplicitParam(name="id",value = "评论ID",required = true,dataType = "Long")
    public JsonResult selectByWeiboId(Long id){
        return JsonResult.ok(commentMapper.selectByWeiboId(id));
    }
}
